package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.common.InStoreConstant;
import net.wanho.dto.InStoreConfirmDTO;
import net.wanho.dto.InStoreDTO;
import net.wanho.dto.InStoreExportDTO;
import net.wanho.mapper.InStoreMapper;
import net.wanho.po.InStore;
import net.wanho.po.Product;
import net.wanho.service.InStoreService;
import net.wanho.service.ProductService;
import net.wanho.vo.InStoreExportVO;
import net.wanho.vo.InStoreVO;
import net.wanho.vo.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 32033
 * @description 针对表【in_store(入库单表)】的数据库操作Service实现
 * @createDate 2025-01-11 17:01:23
 */
@Service
@RequiredArgsConstructor
public class InStoreServiceImpl extends ServiceImpl<InStoreMapper, InStore>
        implements InStoreService {

    private final ProductService productService;

    @Override
    public PageInfo<InStoreVO> getInStorePageList(Integer pageNum, Integer pageSize, InStoreDTO inStoreDTO) {
        Page<InStoreVO> page = new Page<>(pageNum, pageSize);

        QueryWrapper<InStoreVO> wrapper = new QueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(inStoreDTO.getStoreId()), "i.store_id", inStoreDTO.getStoreId());
        wrapper.like(!ObjectUtils.isEmpty(inStoreDTO.getProductName()), "p.product_name", inStoreDTO.getProductName());
        wrapper.ge(!ObjectUtils.isEmpty(inStoreDTO.getStartTime()), "i.create_time", inStoreDTO.getStartTime());
        wrapper.le(!ObjectUtils.isEmpty(inStoreDTO.getEndTime()), "i.create_time", inStoreDTO.getEndTime());

        Page<InStoreVO> pageInfo = this.baseMapper.selectInStorePage(page, wrapper);


        PageInfo<InStoreVO> pageInfo2 = new PageInfo<>();
        pageInfo2.setResultList(page.getRecords());
        pageInfo2.setPageCount(page.getPages());
        pageInfo2.setTotalNum(page.getTotal());
        pageInfo2.setPageSize(page.getSize());
        pageInfo2.setPageNum(page.getCurrent());
        pageInfo2.setLimitIndex(page.offset());

        return pageInfo2;
    }

    @Override
    public void updateInStoreIsIn(InStoreConfirmDTO inStoreConfirmDTO) {
        InStore inStore = this.baseMapper.selectById(inStoreConfirmDTO.getInsId());
        if (inStore == null)
            throw new RuntimeException("入库单不存在");
        if (inStore.getIsIn().equals("1"))
            throw new RuntimeException("该入库单已入库");

        inStore.setIsIn(InStoreConstant.IN_STORE_IS_IN);

        this.baseMapper.updateById(inStore);

        Product product = productService.getById(inStore.getProductId());
        product.setProductInvent(product.getProductInvent() + inStore.getInNum());
        productService.updateById(product);


    }

    @Override
    public List<InStoreExportVO> exportTable(Integer pageNum, Integer pageSize, InStoreExportDTO inStoreExportDTO) {
        // 分页查询
        Page<InStoreExportVO> page = new Page<>(pageNum, pageSize);

        // 构建查询条件，注意指定表别名
        QueryWrapper<InStore> wrapper = new QueryWrapper<>();
        wrapper.eq(inStoreExportDTO.getStoreId() != null, "i.store_id", inStoreExportDTO.getStoreId())
                .ge(StringUtils.hasText(inStoreExportDTO.getStartTime()), "i.create_time", inStoreExportDTO.getStartTime())
                .le(StringUtils.hasText(inStoreExportDTO.getEndTime()), "i.create_time", inStoreExportDTO.getEndTime());

        Page<InStoreExportVO> pageResult = baseMapper.selectExportList(page, wrapper);

        // 返回列表
        return pageResult.getRecords();


    }
}




